<?php
namespace app\shop\controller\goods\ozon;

use app\common\model\shop\AccountModel;
use app\common\model\shop\BrandModel;
use app\common\model\shop\CategoryModel;
use app\common\model\shop\ozon\OzonBrandModel;
use app\common\model\shop\ozon\OzonGoodsCategoryModel;
use app\common\model\shop\PlatformModel;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\App;

/**
 * OZON品牌管理
 * @package app\admin\controller\goods\ozon
 * @author xuli
 * @DateTime 2024-10-29 14:10
 */
class Brand extends Controller
{

    /**
     * 品牌列表
     * @auth true
     * @menu true
     * @author xuli
     * @DateTime 2024-10-29 14:10
     * @return void
     */
    public function index()
    {
        OzonBrandModel::mQuery()->layTable(function () {
            $this->title = lang('品牌管理');
            $this->status_list = OzonBrandModel::sync_status();

        }, function (QueryHelper $query) {
            $this->check_account = false;
            $where = [
                ['is_deleted', '=', 0],
            ];
            $status = $this->request->get('status', '');
            if($status != '') {
                $where[] = ['status', '=', $status];
            }
            $query->where($where);
            // 数据列表搜索过滤
            $query->like('name')->equal('is_deleted,status')->dateBetween('create_at');
        });
    }

    protected function _index_page_filter(&$data)
    {
        $category_ids = array_column($data, 'category_id');
        $category_data = [];
        if(!empty($category_ids)){
            $category_data = OzonGoodsCategoryModel::mk()->whereIn('type_id', $category_ids)->column('type_id,name', 'type_id');
        }
        foreach ($data as &$vo) {
            $vo['status_name'] = OzonBrandModel::sync_status()[$vo['status']] ?? ''; // 同步状态
            $vo['category_name'] = !empty($category_data[$vo['category_id']]['name']) ? $category_data[$vo['category_id']]['name'] : '';
        }
    }

    /**
     * 添加
     * @auth true
     * @menu true
     * @return void
     */
     public function add()
     {
        OzonBrandModel::mForm();
     }

    /**
     * 编辑
     * @auth true
     * @menu true
     * @return void
     */
     public function edit()
     {
        OzonBrandModel::mForm('add');
     }

    protected function _form_filter(&$data)
    {
        if($this->request->isPost()) {
            $params = $this->request->param();
            // 验证
            $this->_vali([
                'name.require'       => lang('品牌名称不能为空'),
                'name.length:1,100'  => lang('品牌名称长度不能超过100'),
                'category_id.gt:0'   => lang('请选择分类'),
            ]);

            $data = [
                'name' => $params['name'],
                'category_id' => $params['category_id'],
                'creater' => $this->adminUser['username'],
                'user_id' => $this->adminUser['id'],
            ];

            if(empty($params['id'])) { // 添加
                $result = OzonBrandModel::mq()->insertGetId($data);
            } else { // 编辑
                unset($data['user_id'], $data['creater'], $data['platform_id']);
                $data['updater'] = $this->adminUser['username'];
                $result = OzonBrandModel::mq()->where(['id' => $params['id']])->update($data);
            }

            if($result) {
                $this->success(lang('保存成功'));
            } else {
                $this->error(lang('保存失败'));
            }
        } else {
            $this->category_id = $data['category_id'] ?? 0;
        }
    }

    /**
     * 删除
     * @auth true
     * @menu true
     * @return void
     */
    public function delete()
    {
        $id = $this->request->param('id', 0);
        $info = OzonBrandModel::mq()->find($id);
        if(!$info) {
            $this->error('店铺信息错误');
        }
        try {
            $result = OzonBrandModel::mq()->where(['id' => $id])->update(['is_deleted' => 1]);
            if(!$result){
                throw new \Exception(lang('删除失败'));
            }
        } catch (\Exception $e) {
            $this->error(lang('删除失败'));
        }
        $this->success(lang('删除成功'));
    }


    /**
     * 获取分类列表
     * @Description
     * @author wangqinghui
     * @date 2024-12-11 09:03
     * @return void
     */
    public function getCategoryList()
    {
        // 获取系统分类
        $category_data = OzonGoodsCategoryModel::getCategoryTree();
        if(empty($category_data)){
            $where = [
                ['status', '=', 0],
            ];
            $category_list = OzonGoodsCategoryModel::mk()->where($where)->field('name title,id,pid,path,type_id')->select()->toArray();
            $category_data = OzonGoodsCategoryModel::categoryTree($category_list);
        }
        $this->success('获取分类列表', $category_data);
    }

    /**
     * 同步品牌信息
     * @Description
     * @author wangqinghui
     * @date 2024-12-11 15:23
     * @return void
     */
    public function pullBrand()
    {
        $this->_queue('异步拉取品牌信息', 'ozonAttribute brand');
    }
}